$(function(){
	var ctxPath = $('body').attr('ctxPath');
	var openId = $('#openId').val();
	
	$('#cardNavbar').tab({
		defaultIndex : 0,
		activeClass : "tab-blue"
	});
	
	//页面加载完执行
	//默认获取所有个人名片
	getAllMyPersonalCards(openId);
	
	
	//点击查看个人名片
	$("#personalCard").on('click',function() {
		getAllMyPersonalCards(openId);
		$("#myPersonalCards").show().siblings().hide();
	});
	
	//点击查看企业名片
	$("#companyCard").on('click',function() {
		getAllMyCompanyCards(openId);
		$("#myCompanyCards").show().siblings().hide();
	});

	//点击单张个人名片查看详细信息
	$("#personalCards").on('click','.my_card_myItem_l',  function(){
		var id = $(this).attr("data-id");
		window.location.href=ctxPath+"/basic/my/"+id+"?_t="+new Date().getTime();
	});
	
	//点击单张企业名片查看详细信息
	$("#companyCards").on('click','.my_card_myItem_l',  function(){
		var id = $(this).attr("data-id");
		window.location.href=ctxPath+"/company/view/"+id+"?_t="+new Date().getTime();
	});
	
	//点击新增个人名片
	$("#addPersonalCard").on('click',function(){
		//跳转到新增个人名片简易界面
		if(openId == null){
			$.toptips("网络开小差了:(");
			return;
		}
		window.location.href=ctxPath + '/basic/simple/'+openId+'?_t='+new Date().getTime();
		
	});
	
	//点击新增企业名片
	$("#addCompanyCard").on('click',function(){
		if(openId == null){
			$.toptips("网络开小差了:(");
			return;
		}
		window.location.href=ctxPath + '/company/create/'+openId+'?_t='+new Date().getTime();
	});
	
	//点击名片夹转换
	$("#list_alone1").click(function() {
		$(this).addClass("my_card_item_style").siblings().removeClass("my_card_item_style");
		$("#list_alone").show().siblings().hide();
	});
	$("#list_company1").click(function() {
		$(this).addClass("my_card_item_style").siblings().removeClass("my_card_item_style");
		$("#list_company").show().siblings().hide();
	});
	/*$('.weui_tab').tab({
	    defaultIndex: 0,
	    activeClass:'weui_bar_item_on',
	    onToggle:function(index){
				if(index>0){
//						$.toptips(index);
					}
				}
		  });*/
	  
	$('.searchbar_wrap').searchBar({
	    cancelText:"取消",
	    searchText:'关键字',
	    /*onfocus: function (value) {
	    	$.toptips(value);
	    },*/
	    onblur:function(value) {
	    	$.toptips(value);
	    }/*,
	    oninput: function(value) {
	    	$.toptips(value);
	    },
	    onsubmit:function(value){
	    	$.toptips(value);
	    },
	    oncancel:function(){
	    	$.toptips(value);
	    },
	    onclear:function(){
	    	$.toptips(value);
	    }*/
	});
});

//获取所有个人名片
function getAllMyPersonalCards(openId){
	$.ajax({
		type:'post',
		url:$('body').attr('ctxPath')+'/card/my/personal/'+openId+'?_t='+new Date().getTime(),
		dataType:'json',
		success:function(data){
			if(data.flag){
//				console.log(data.list);
				$("#personalCards").empty();
				$(data.list).each(function(index,obj){
					$('#personalCards').prepend('<div class="my_card_myItem" ><div class="my_card_myItem_l" data-id="'+obj.id+'"><div class="my_card_myItem_content my_card_content_size"><span class="icon icon-84"></span><span>'+obj.userName+'</span></div></div><div class="my_card_myItem_r"><div class="my_card_myItem_content"><span class="icon icon-64"></span><span>105</span><span class="icon icon-84"></span><span>15</span><span class="icon icon-101"></span></div></div></div>');
				})
			}else{
				$('#personalCards').prepend('<div class="my_card_myItem">'+data.msg+'</div>');
			}
		},
		error:function(error){
			console.log(error);
			$.toptips("网络开小差了:(");
		}
	});
}

//获取所有公司名片
function getAllMyCompanyCards(openId){
	$.ajax({
		type:'post',
		url:$('body').attr('ctxPath')+'/card/my/company/'+openId+'?_t='+new Date().getTime(),
		dataType:'json',
		success:function(data){
			if(data.flag){
				$("#companyCards").empty();
				$(data.list).each(function(index,obj){
					$("#companyCards").prepend('<div class="my_company_item"><div class="my_card_myItem_l" data-id="'+obj.id+'"><div class="my_card_myItem_content my_card_content_size"><span class="icon icon-100"></span><span>'+obj.companyName+'</span></div></div><div class="my_card_myItem_r"><div class="my_card_myItem_content"><span class="icon icon-64"></span><span>105</span><span class="icon icon-84"></span><span>15</span><span class="icon icon-101"></span></div></div></div>');
				})
				}else{
					$('#companyCards').prepend('<div class="my_card_myItem">'+data.msg+'</div>');
			}
		},
		error:function(error){
			console.log(error);
			$.toptips("网络开小差了:(");
		}
	});
}